// 加载 face-api.js
importScripts(chrome.runtime.getURL('lib/face-api.min.js'));

// 监听消息
self.onmessage = async function(e) {
  if (e.data.type === 'detect') {
    try {
      // 加载模型
      if (!self.modelLoaded) {
        await faceapi.nets.ssdMobilenetv1.loadFromUri(chrome.runtime.getURL('models'));
        self.modelLoaded = true;
      }

      // 检测人脸
      const img = await faceapi.fetchImage(e.data.imageUrl);
      const detections = await faceapi.detectAllFaces(img);
      
      // 返回结果
      self.postMessage({
        type: 'result',
        imageId: e.data.imageId,
        hasOneFace: detections.length === 1
      });
    } catch (error) {
      self.postMessage({
        type: 'error',
        imageId: e.data.imageId,
        error: error.message
      });
    }
  }
}; 